import mysql from "mysql"
export default class user {
    static adduser(data) {
        var connection = mysql.createConnection({
            host: 'localhost',
            user: 'root',
            password: 'root',
            port: '3306',
            database: 'test'
        });
        if(data.split('{')[0]!=='')data=data.match(/(\{.*?\})/)[1]
        data=JSON.parse(data)
        connection.connect();
        var addSql = 'INSERT INTO test.user(username,password,nickname) VALUES('+'"'+data.username+'"'+','+'"'+data.password+'"'+','+'"'+data.nickname+'"'+')';
        var addSqlParams = [data.username, data.password, data.nickname];
        connection.query(addSql,addSqlParams);

        connection.end();
    }
    static deluser(data) {
        var connection = mysql.createConnection({
            host: 'localhost',
            user: 'root',
            password: 'root',
            port: '3306',
            database: 'test'
        });
        // data=JSON.parse(data)
        // console.log(data);
        // var _data;
        if(data.split('{')[0]!=='')data=data.match(/(\{.*?\})/)[1]
        data=JSON.parse(data)
        connection.connect();
        var delSql = 'DELETE FROM test.user where username='+"'"+data.username+"'";
        //增
        // console.log(addSql);
        connection.query(delSql);

        connection.end();
    }
    static changeuser(data) {
        var connection = mysql.createConnection({
            host: 'localhost',
            user: 'root',
            password: 'root',
            port: '3306',
            database: 'test'
        });
        if(data.split('{')[0]!=='')data=data.match(/(\{.*?\})/)[1]
        data=JSON.parse(data)
        
        connection.connect();
        var modSql = "UPDATE test.user SET password=?,nickname=? WHERE username=?";
        var modSqlParams = [data.password,data.nickname,data.username];
        console.log(modSqlParams);
        connection.query(modSql,modSqlParams);

        connection.end();
    }
}